package gui;

import java.util.Vector;

import javax.bluetooth.DataElement;
import javax.bluetooth.ServiceRecord;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;

import logic.BluetoothServerConnectionHandler;
import logic.LogicHelper;
import logic.MainMidlet;

/**
 * screen for showing results of bluetooth search
 * @author Rekin
 *
 */
public class SearchResultScreen extends List implements CommandListener{

	private Vector serviceRecordsFound;
	
	/**
	 * constructor
	 * @param availableServices vector of found services to be shown
	 */
	public SearchResultScreen(Vector availableServices){
		super("", List.EXCLUSIVE);
		serviceRecordsFound = availableServices;
		if(serviceRecordsFound.size()>0){
			setTitle("Select devices to connect:");
			addFoundItemsToList();
		}
		else{
			setTitle("No clients found");
		}
		createCommands();
	}
	
	/**
     * handling for user actions
     */
	public void commandAction(Command c, Displayable d) {
		if(c.getCommandType()==Command.EXIT){
        	MainMidlet.getMidletInstance().showMainMenu();
        }
        else if(c.getCommandType()==Command.OK){
        	if(c.getLabel().compareTo("Start game") == 0){
        		handleStartGame();
        	}
        	else if(c.getLabel().compareTo("Search again") == 0){
        		MainMidlet.getMidletInstance().setDisplay(
            			new WaitingScreen(LogicHelper.serverWaitingScreen, 
            					"Searching for players...", "Please wait..."));
        	}
        }
	}
	
	/**
	 * adds listener for buttons
	 */
	private void createCommands(){
		if(serviceRecordsFound.size()>0){
			Command startGame = new Command("Start game", Command.OK, 1);
			addCommand(startGame);
		}
		Command searchAgain = new Command("Search again", Command.OK, 2);
		Command mainMenu = new Command("Go back", Command.EXIT, 3);
		addCommand(searchAgain);
		addCommand(mainMenu);
		setCommandListener(this);
	}
	
	/**
	 * adds elements to list
	 */
	private void addFoundItemsToList(){
		for(int i=0;i<serviceRecordsFound.size();++i){
			ServiceRecord sr = (ServiceRecord)serviceRecordsFound.elementAt(i);
			DataElement de = sr.getAttributeValue(LogicHelper.SERVICE_NAME_PARAM);
			String rname = (String)de.getValue();
			append(rname, null);
		}
	}
	
	/**
	 * handles game start
	 */
	private void handleStartGame(){
		Vector selectedRecords = new Vector();
		boolean isAnyElementSelected = false;
		for(int i=0;i<serviceRecordsFound.size();++i){
			if(isSelected(i)){
				selectedRecords.addElement(serviceRecordsFound.elementAt(i));
				isAnyElementSelected = true;
				break; //we connect only one device
			}
		}
		if(!isAnyElementSelected){
			return;
		}
		//show next waiting screen
		MainMidlet.getMidletInstance().setDisplay(new ServerConnectingToClientWaitingScreen());
		BluetoothServerConnectionHandler handler= new BluetoothServerConnectionHandler(selectedRecords);
		handler.start();
	}
}
